package com.lzy.cloud.common.controller;

import com.github.pagehelper.PageInfo;
import com.lzy.cloud.common.dtos.ParamDto;
import com.lzy.cloud.common.request.ParamRequest;
import com.lzy.cloud.common.response.ParamResponse;
import com.lzy.cloud.common.service.ParamService;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/param")
@RestController
public class ParamController {

    @Autowired
    private ParamService paramService;

    /**
     * 根据paramKey查询
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/detail/{paramKey}", method = RequestMethod.GET)
    public Result selectByKey(@PathVariable("paramKey") String paramKey) {
        Ensure.that(paramKey).isNotNull("00000002");
        ParamDto paramDto = paramService.selectByKey(paramKey);
        Ensure.that(paramDto).isNotNull("00000006");
        return new Result(DozerUtils.map(paramService.selectByKey(paramKey), ParamResponse.class));
    }

    /**
     * 查询所有
     */
    @RequestMapping(value = "/find", method = RequestMethod.POST)
    public Result selectParamList(@RequestBody ParamRequest request) {
        Pager pager = new Pager();
        pager.setPageNum(request.getPageNum());
        pager.setPageSize(request.getPageSize());
        PageInfo<ParamDto> pageInfo = paramService.selectParamList(pager, DozerUtils.map(request, ParamDto.class));
        return new Result(pageInfo);
    }

    /**
     * 增加
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result addParam(@RequestBody ParamRequest paramRequest) {
        Ensure.that(paramRequest).isNotNull("00000002");
        Boolean flag  = paramService.addParam(DozerUtils.map(paramRequest, ParamDto.class));
        Ensure.that(flag).isTrue("00000003");
        return new Result(flag);
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result update(@RequestBody ParamRequest paramRequest) {
        Ensure.that(paramRequest).isNotNull("00000002");
        Boolean flag = paramService.update(DozerUtils.map(paramRequest,ParamDto.class));
        Ensure.that(flag).isTrue("00000004");
        return new Result(flag);
    }

    /**
     * 刪除
     */
    @RequestMapping(value = "/delete/{ids}", method = RequestMethod.GET)
    public Result deleteParam(@PathVariable("ids") String ids) {
        Ensure.that(ids).isNotNull("00000002");
        Boolean flag = paramService.deleteParam(ids);
        Ensure.that(flag).isTrue("00000005");
        return new Result(flag);
    }
}
